package com.wuj.predicate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
 * 自定义谓词工厂,业务模拟12306 晚上23:00到凌晨6:00不能购票
 *
 * @author wuj
 * @date 2019/12/16
 */
@Component
@Slf4j
public class CustomTimeBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<TimeBetweenConfig> {

    public CustomTimeBetweenRoutePredicateFactory() {
        super(TimeBetweenConfig.class);
    }


    @Override
    public Predicate<ServerWebExchange> apply(TimeBetweenConfig config) {

        LocalTime startTime = config.getStartTime();
        LocalTime endTime = config.getEndTime();

        return serverWebExchange -> {
            LocalTime now = LocalTime.now();
            return  now.isAfter(startTime)&&now.isBefore(endTime);
        };
    }


    @Override
    public List<String> shortcutFieldOrder() {
        //从配置文件中提取数组转化为为配置
        return Arrays.asList("startTime","endTime");
    }
}
